package com.devin.controller;

import com.devin.model.dto.ChatMessage;
import com.devin.model.enums.MessageType;
import io.netty.util.internal.SuppressJava6Requirement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;

/**
 * @author: simpl
 * @date: 2020/3/30
 * @time: 12:50
 * @description: 
 */


@Slf4j
@Controller
public class ChatController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;



    /**
    *@Description: 房间聊天
    *@Param:
    *@return:
    *@date: 2020/4/1
    */
    @MessageMapping("/{roomId}/chat")
//    @SendTo("/topic/public")
    public void sendMessage( @Payload ChatMessage chatMessage,@DestinationVariable String roomId) {

        simpMessagingTemplate.convertAndSend("/topic/"+roomId, chatMessage);
        return ;
    }


    /**
    *@Description:   控制播放器所需要进行的操作
    *@Param:
    *@return:
    *@date: 2020/4/1
    */
    @MessageMapping("/{roomId}/operator")
    public void operator(
            @DestinationVariable String roomId,
            @Payload ChatMessage chatMessage ){

            log.info("-----------------");
            log.info(chatMessage.getContent());
            log.info("-----------------");
        simpMessagingTemplate.convertAndSend("/topic/"+roomId,chatMessage);
    }

    /**
    *@Description:   进入房间 需要做的操作
    *@Param:
    *@return:
    *@date: 2020/4/1
    */
    @MessageMapping("/{roomId}/enter")
//    @SendTo("/topic/{roomId}")
    public void addUser(@Payload ChatMessage chatMessage,
                               @DestinationVariable String roomId,
                               SimpMessageHeaderAccessor headerAccessor) {
        log.info("用户::" + chatMessage.getSender() + " enter : room :" + roomId);
        headerAccessor.getSessionAttributes().put("roomId", roomId);
        headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());

        ChatMessage enterMessage = new ChatMessage();
        enterMessage.setType(MessageType.ENTER.getType());
        enterMessage.setSender(chatMessage.getSender());
        //看能否发送到  房间 暂时不用注解
        simpMessagingTemplate.convertAndSend("/topic/"+roomId,enterMessage);
        return ;
    }



}
